import {
	observable,
	action
} from 'mobx';
import * as mobx from 'mobx'
import * as Api from '../apis/systemCard';
import {
	WeaForm
} from 'comsMobx';
import {
	WeaTableNew
} from 'comsMobx'
import {
	message,
} from 'antd'
import {i18n} from '../public/i18n';
const {
	TableStore
} = WeaTableNew;

export class HrmSystemCard {
	@observable tabKey = '1';
	@observable headerColumns = [];
	@observable sessionkey = '';
	@observable condition1 = [];
	@observable condition2 = [];
	@observable form1 = new WeaForm();
	@observable form2 = new WeaForm();
	@observable isPanelShow = false;
	@observable isSecSettingShow = false;
	@observable tableStore = new TableStore();
	@observable isPassVerification = false;
	@observable isSecSetting = false;
	@observable loginId = '';
	@observable hrmId = '';
	@observable hrmName = '';
	@observable isSecondaryVerifyUsed = false;
	@observable isSecondaryVerifySetted = false;
	@observable hasRight = false;
	@observable loading = false;

	@action
	tabClick(key) {
		this.tabKey = key;
		this.getHeaderColumn(key);
		this.updateFormFields('', '1');
		this.updateFormFields(key, '2');
		this.updateFormFields('', '3');
		this.getTableInfo(key);
		this.hideSearchAd();
	}

	@action getHasRight = (id) => {
		this.loading = true;
		this.userid = id;
		const params ={}
		if (id) {
			params.userid=id
		}
		Api.getHasRight(params).then((data) => {
			const {
				hasRight
			} = data;

			this.hasRight = hasRight;

			this.loading = false;

			if (hasRight) {
				this.setShowSearchAd(false);
				this.getHeaderColumn(this.tabKey);
				this.getSearchCondition();
				this.getTableInfo('1');
			}else{
				document.title = i18n.label.resourceCard()
			}
		});
	}

	getHeaderColumn(key) {
		let params = {
			type: key
		}
		if (this.userid) {
			params.userid = this.userid
		}
		Api.getHeaderColumn(params).then((data) => {
			if (data) {
				this.headerColumns = data;
			}
		});
	}

	getTableInfo(key) {
		let params;
		if (key) {
			params = {
				type: key
			}
		} else {
			params = this.form1.getFormParams();
		}
		if ('type' in params && params.type !== '6' && params.type !== '8') this.tabKey = params.type;
		if ('type' in params && (params.type == '6' || params.type == '8')) this.tabKey = '0';
		if (this.userid) {
			params.userid = this.userid
		}
		this.tableStore = new TableStore();
		Api.getTableInfo(params).then((data) => {
			this.tableStore.getDatas(data.sessionkey);
		});
	}

	getSearchCondition() {
		const params = {}
		if (this.userid) {
			params.userid = this.userid
		}
		Api.getSearchCondition(params).then((data) => {
			data.condition.forEach(c => {
				c.title = ''
				c.items = c.items.slice(0,2)
			})
			this.form1.setCondition(data.condition);

			// this.condition1 = data.condition;
			this.hrmName = data.hrmName;
			// this.form1 = new WeaForm();
			// this.form1.initFormFields(data.condition);
    		document.title = i18n.label.resourceCard() + '-' + data.hrmName;
		});
	}

	getSecSettingCondition(params) {
		if (this.userid) {
			params.userid = this.userid
			params.id =  this.userid
		}
		Api.getSecSettingCondition(params).then((data) => {
			this.isSecSetting = data.secSettings;
			this.condition2 = data.condition;
			this.hrmId = data.hrmId;
			this.form2 = new WeaForm();
			this.form2.initFormFields(data.condition);
			//获取二次验证密码信息：是否设置、是否启用
			this.getSecondaryVerifyPwdInfo();
		});
	}

	//获取二次验证密码信息：是否设置、是否启用
	getSecondaryVerifyPwdInfo = () => {
		const params = {
			id: this.hrmId, //系统管理员id为1
		}
		if (this.userid) {
			params.userid = this.userid
		}
		Api.isUseSecondaryPwd(params).then(cb => {
			const {
				isSetted, //是否设置
				isUse, //是否启用
			} = cb;

			this.isSecondaryVerifySetted = isSetted;
			this.isSecondaryVerifyUsed = isUse;
		});
	}

	//保存二次验证密码设置
	saveSecondaryPasswordSetting = (useSecondaryPwd) => {
		const params = {
			id: this.hrmId, //系统管理员id为1
			useSecondaryPwd, //useSecondaryPwd:true -> 启用 false -> 停用
		}
		if (this.userid) {
			params.userid = this.userid
		}
		Api.saveUseSecondaryPwd(params).then(cb => {
			const {
				sign
			} = cb;

			if (sign !== '1') {
				message.warning(cb.message);
				return
			}

			// this.isSecondaryVerifyUsed = useSecondaryPwd;
			this.getSecondaryVerifyPwdInfo();
		});
	}

	setShowSearchAd(bool) {
		this.isPanelShow = bool;
	}

	hideSearchAd() {
		this.isPanelShow = false;
	}

	setDialogShow() {
		this.isSecSettingShow = true;
	}

	hideDialog() {
		this.isSecSettingShow = false;
	}

	reset() {
		this.form2.reset();
	}

	setLoginId(loginId) {
		this.loginId = loginId;
	}

	updateFormFields(v, f) {
		if (f == '1') {
			this.form1.updateFields({
				relatedname: {
					value: v
				}
			});
		}
		if (f == '2') {
			this.form1.updateFields({
				type__startdate__startdateto: {
					value: [v]
				}
			});
		}

		if (f == '3') {
			this.form1.updateFields({
				typeid: {
					value: [v]
				}
			});
		}
	}

	save() {
		let params = {
			loginid: this.loginId,
			...this.form2.getFormParams()
		};
		if (this.userid) {
			params.userid = this.userid
			params.id = this.userid
		}

		Api.secSettingSave(params).then((data) => {
			if (data.status == '1') {
				message.success('安全设置项保存成功!');
				this.hideDialog();
			} else {
				message.success('安全设置项保存失败!');
			}
		});
	}
}